package com.example;

import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import com.example.beat.BeatGenerator;
import com.example.beat.IBeatGenerator;
import com.example.beat.Observer;
import com.example.controler.Controler;

/**
 * Created by IntelliJ IDEA.
 * User: Eugeny
 * Date: 10.04.12
 * Time: 18:22
 * To change this template use File | Settings | File Templates.
 */
public class GameActivity extends Activity implements Observer, Controler {

    //model
    IBeatGenerator beat;        //beat      - rhythm ;)
    //IPlayer hero;             //hero      - some monster ;)
    //IItemGenerator generator; //generator - bombs's mother ;)

    //controler
    ImageButton pause;
    ImageButton zoom;
    ImageButton ib1;
    ImageButton ib2;
    ImageButton ib3;
    ImageButton ib4;

    //view
    View model;

    //
    int controlState = 1;      //default state is 1
    boolean isZoomed = false;  //default zoom is zoomin


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //set the view and not only ;)
        init(R.layout.wolf_zoomin);
    }

    public void init(int layout){
        //first of all - set the view
        setContentView(layout);
        model = findViewById(R.id.game);
        pause = (ImageButton) findViewById(R.id.pause);
        zoom  = (ImageButton) findViewById(R.id.zoom);
        ib1   = (ImageButton) findViewById(R.id.btn1);
        ib2   = (ImageButton) findViewById(R.id.btn2);
        ib3   = (ImageButton) findViewById(R.id.btn3);
        ib4   = (ImageButton) findViewById(R.id.btn4);

        pause.setOnClickListener(pauseListener);
        zoom. setOnClickListener(zoomListener);
        ib1.  setOnClickListener(ib1Listener);
        ib2.  setOnClickListener(ib2Listener);
        ib3.  setOnClickListener(ib3Listener);
        ib4.  setOnClickListener(ib4Listener);
    }

    @Override
    public void onStart(){
        super.onStart();

        //initialized all fields
        beat = BeatGenerator.getInstance();
        //generator = Generator.getInstance();
        //hero = new HER();

        //register all by beat
        beat.registerObserver(this);
        //beat.registerObserver(generator)
        //beat.registerObserver(hero);

        //beat go go go
        beat.startGenerator();
    }

    @Override
    public void onPause(){
        super.onPause();
        //want to pause? ok! beat is paused too
        beat.pauseGenerator();
    }

    @Override
    public void onStop(){
        super.onStop();
        //want to stop? ok! beat is stopped too
        beat.stopGenerator();
    }

    @Override
    public void onDestroy(){
        super.onDestroy();
        //want to destroy? ok! beat is stopped... wtf? stop == destroy? ok, then onResume(){getInstance of beat and start}
        beat.stopGenerator();
    }

    private final View.OnClickListener ib1Listener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            controlState = 1;
        }
    };

    private final View.OnClickListener ib2Listener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            controlState = 2;
        }
    };

    private final View.OnClickListener ib3Listener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            controlState = 3;
        }
    };

    private final View.OnClickListener ib4Listener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            controlState = 4;
        }
    };

    private void wolfGo(int i){
        //hero.changeState(i)
        //then hero notify model, and model change its background
        switch (i){
            case 1:
                model.setBackgroundResource(R.drawable.wolf3);
                break;
            case 2:
                model.setBackgroundResource(R.drawable.wolf1);
                break;
            case 3:
                model.setBackgroundResource(R.drawable.wolf4);
                break;
            case 4:
                model.setBackgroundResource(R.drawable.wolf2);
                break;
            default:
                break;
        }
    }

    private final View.OnClickListener pauseListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            //wolfGo(lastControlUsed());
        }
    };

    private final View.OnClickListener zoomListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (isZoomed){
                Drawable d = model.getBackground();
                init(R.layout.wolf_zoomin);
                model.setBackgroundDrawable(d);
            } else {
                Drawable d = model.getBackground();
                init(R.layout.wolf_zoomout);
                model.setBackgroundDrawable(d);
            }
            isZoomed = !isZoomed;
        }
    };

    public int lastControlUsed(){
        return controlState;
    }

    @Override
    public void update(int currentBeat) {
        //model.DO WHAT YOU NEED TO DO
        wolfGo(lastControlUsed());
        //eggGenerator.DO SOMETHING
    }
}
